<?php

// +-----------------------------------------------------------------------
// | @Copyright (c) 2012 http://t00ls.net.
// +-----------------------------------------------------------------------
// | @Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +-----------------------------------------------------------------------
// | @author: lenush <jnicklasj@gmail.com> qq:707207845
// +-----------------------------------------------------------------------

final class Gos_Controller_Student extends Gospel_Controller_Api {

    public function unitTest() {
        
    }

    public function index() {
        $course = new Gos_Model_Course();
        $cwhere = '1=1';
        $coursedata = $course->getItems($cwhere, 'id');
        $grade = new Gos_Model_Grade();
        $gwhere = '1=1';
        $gradedata = $grade->getItems($gwhere, 'id');
        $searchfor = $this->getRequest()->getPost('searchfor');
        $cId = $this->getRequest()->getString('cId');
        $status = $this->getRequest()->getInt('status');
        $p = $this->getRequest()->getInt('parameters') == 0 ? 1 : $this->getRequest()->getInt('parameters');
        $students = $this->students($searchfor, $cId, $status, $p);
        $teacher = new Gos_Model_Account();
        $twhere = '1=1';
        $teacherdata = $teacher->getItems($twhere, 'id');
        //课程页面文字输出
        foreach ($coursedata as $k => $v) {
            $id[] = $v['id'];
            $name[] = $v['cName'];
        }
        $coursearr = array_combine($id, $name);
        //班级页面文字输出
        foreach ($gradedata as $kk => $vv) {
            $ids[] = $vv['id'];
            $names[] = $vv['gname'];
        }
        $gradearr = array_combine($ids, $names);
        $this->tpl->assign('course', $coursedata);
        $this->tpl->assign('coursearr', $coursearr);
        $this->tpl->assign('grade', $gradedata);
        $this->tpl->assign('gradearr', $gradearr);
        $this->tpl->assign('teacher', $teacherdata);
        $this->tpl->assign('students', $students['data']);
        $students['pager']['ptotal'] = ceil($students['pager']['rtotal'] / $students['pager']['psize']);
        $this->tpl->assign('pager', $students['pager']);
        $this->tpl->draw('student/index');
    }

    public function show() {
        
    }

    public function news() {
        
    }

    public function create() {
        $return = true;
        $array = null;
        $array['uName'] = $this->getRequest()->getPost('uName');
        $array['uFather'] = $this->getRequest()->getPost('uFather');
        $array['uFathertel'] = $this->getRequest()->getPost('uFathertel');
        $array['uMother'] = $this->getRequest()->getPost('uMother');
        $array['uMothertel'] = $this->getRequest()->getPost('uMothertel');
        $array['cId'] = $this->getRequest()->getPost('cId');
        $array['gid'] = $this->getRequest()->getPost('gId');
        $array['tId'] = $this->getRequest()->getPost('tId');
        $array['uPayfee'] = $this->getRequest()->getPost('uPayfee');
        //课程费用关联
        $array['uTotalfee'] = $this->getRequest()->getPost('uTotalfee');
        $array['uSchool'] = $this->getRequest()->getPost('uSchool');
        $array['uBirth'] = $this->getRequest()->getPost('uBirth');
        $array['uSex'] = $this->getRequest()->getPost('uSex');
        $array['uAge'] = $this->getRequest()->getPost('uAge');
        $array['uWhere'] = $this->getRequest()->getPost('uWhere');
        $array['uFrom'] = $this->getRequest()->getPost('uFrom');
        $array['uMemo'] = $this->getRequest()->getPost('uMemo');
        $array['createat'] = time();
        try {
            $module = new Gos_Model_Student();
            if (false == $module->append($array)) {
                $return = false;
            }
        } catch (Gospel_Db_Exception $mde) {
            throw $mde;
        }
        if ($return) {
            $this->message('数据新增成功，3秒后返回！', '/Student/index');
        }
    }

    public function edit() {
        $return = null;
        $course = new Gos_Model_Course();
        $cwhere = 'isdestory=0';
        $coursedata = $course->getItems($cwhere, 'id');
        $grade = new Gos_Model_Grade();
        $gwhere = 'isdestory=1';
        $gradedata = $grade->getItems($gwhere, 'id');
        $id = $this->getRequest()->getInt('parameters');
        $where = sprintf("id=%d", $id);
        try {
            $module = new Gos_Model_Student();
            $return = $module->getItem($where);
        } catch (Gospel_Db_Exception $mde) {
            throw $mde;
        }
        $this->tpl->assign('course', $coursedata);
        $this->tpl->assign('grade', $gradedata);
        $this->tpl->assign('student', $return);
        $this->tpl->draw('student/edit');
    }

    public function update() {
        $return = true;
        $array = null;
        $array['uName'] = $this->getRequest()->getPost('uName');
        $array['uFather'] = $this->getRequest()->getPost('uFather');
        $array['uFathertel'] = $this->getRequest()->getPost('uFathertel');
        $array['uMother'] = $this->getRequest()->getPost('uMother');
        $array['uMothertel'] = $this->getRequest()->getPost('uMothertel');
        $array['cId'] = $this->getRequest()->getPost('cId');
        $array['gId'] = $this->getRequest()->getPost('gId');
        $array['uPayfee'] = $this->getRequest()->getPost('uPayfee');
        $array['uTotalfee'] = $this->getRequest()->getPost('uTotalfee');
        $array['uSchool'] = $this->getRequest()->getPost('uSchool');
        $array['uBirth'] = $this->getRequest()->getPost('uBirth');
        $array['uSex'] = $this->getRequest()->getPost('uSex');
        $array['uAge'] = $this->getRequest()->getPost('uAge');
        $array['uWhere'] = $this->getRequest()->getPost('uWhere');
        $array['uFrom'] = $this->getRequest()->getPost('uFrom');
        $array['uMemo'] = $this->getRequest()->getPost('uMemo');
        $array['status'] = $this->getRequest()->getPost('status');
        try {
            $where = sprintf("id='%d'", $this->getRequest()->getPost('id'));
            $module = new Gos_Model_Student();
            if (false == $module->alter($where, $array)) {
                $return = false;
            }
        } catch (Gospel_Db_Exception $mde) {
            throw $mde;
        }
        if ($return) {
            $this->message('数据编辑成功，3秒后返回！', '/Student/index');
        }
    }

    public function destory() {
        $return = true;
        try {
            $where = sprintf("id='%d'", $this->getRequest()->getInt('parameters'));
            $array['isdestory'] = 1;
            $module = new Gos_Model_Student();
            if (false == $module->alter($where, $array)) {
                $return = false;
            }
        } catch (Gospel_Db_Exception $mde) {
            throw $mde;
        }
        if ($return) {
            $this->message('数据删除成功，3秒后返回！', '/Student/index');
        }
    }

    public function students($searchfor, $cId, $status, $offset, $sort = 'id DESC', $psize = '15') {
        $return = array();
        $where = sprintf("isdestory = '%d'", 0);
//        $where .= sprintf(" AND isdestory = '%d'", $status);
        if ($cId != '') {
            $where .= sprintf(" AND cId = '%d'", $cId);
        }
        if ($searchfor != '') {
            $where .= sprintf(" AND uName LIKE '%s'", "%$searchfor%");
        }
        try {
            $module = new Gos_Model_Student();
            $where = $where != '' ? $where : '1=1';
//            $return = $module->getItems($where, $sort);
            $offset = $offset == 0 ? 0 : ($offset - 1) * $psize;
            $return = $module->getList($where, $sort, $offset, $psize);
        } catch (Gospel_Db_Exception $mde) {
            throw $mde;
        }
        return $return;
    }

    public function grade() {
        $module = new Gos_Model_Student();
        $gid = $this->getRequest()->getInt('parameters');
        $where = sprintf("gid = '%d'", $gid);
        $students = $module->getItems($where, 'id');
        $course = new Gos_Model_Course();
        $cwhere = '1=1';
        $coursedata = $course->getItems($cwhere, 'id');
        $grade = new Gos_Model_Grade();
        $gwhere = '1=1';
        $gradedata = $grade->getItems($gwhere, 'id');
        //课程页面文字输出
        foreach ($coursedata as $k => $v) {
            $id[] = $v['id'];
            $name[] = $v['cName'];
        }
        $coursearr = array_combine($id, $name);
        //班级页面文字输出
        foreach ($gradedata as $kk => $vv) {
            $ids[] = $vv['id'];
            $names[] = $vv['gname'];
        }
        $gradearr = array_combine($ids, $names);
        $this->tpl->assign('students', $students);
        $this->tpl->assign('coursearr', $coursearr);
        $this->tpl->assign('gradearr', $gradearr);
        $this->tpl->draw('student/grade');
    }

}

/**
* // +---------------------------------------------------------------------
* // | @ Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
* // +---------------------------------------------------------------------
* // | @ author: lenush <jnicklasj@gmail.com> qq:707207845
* // +---------------------------------------------------------------------
* Local variables:
* tab-width:4
* basic-offset:4
* indent-tabs-mode:t
* End:
*/
